#pragma once
#include "IWinhttpWebSocket.h"
#include <functional>

#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")

#include "DbgLog.h"
#pragma comment(lib, "DbgLog.lib")


class WinHttpWebSocket : public IWinHttpWebSocket
{
	
public:
	WinHttpWebSocket(std::wstring strUrl, Recv_CB_T cb, LPVOID lpContext);
	~WinHttpWebSocket();
	static DWORD WINAPI RecvThread(LPVOID lParam);

public:
	bool Init();
	bool CrackUrl();
	bool Open();
	void Close();
	void Release();
	bool AddHeader(std::wstring strHeader);
	bool Send(std::wstring strMessage);

private:
	void ReleaseHandle(HINTERNET handle)
	{
		if (handle)
		{
			WinHttpCloseHandle(handle);
			handle = NULL;
		}
	}

private:
	bool blInited = false;
	HINTERNET hWebsock = NULL;
	HINTERNET hSession = NULL;
	HINTERNET hConnect = NULL;
	HINTERNET hRequest = NULL;

	std::wstring m_strUrl;
	std::wstring m_strPool;
	std::wstring m_strHost;
	std::wstring m_strPort;
	std::wstring m_strPath;

	Recv_CB_T m_RecvCB;
	LPVOID m_RecvContext;
};

